package com.zhang.buiness.amaster.reportserver.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class ReportServiceImpl {
    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand( commandProperties = {
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10")
    },fallbackMethod = "ribbionAloneUseFallback")
    public List<String> ribbionAloneUse(String clientMark, String path) {
        String url  = String.format("http://%s%s",clientMark,path);
        ResponseEntity<List> entity = restTemplate.getForEntity(url, List.class);
        return  entity.getBody();
    }


    //ribbionAloneUse方法降级服务
    public List<String> ribbionAloneUseFallback(String clientMark,String path,Throwable e){
        log.error(e.getMessage(),e);
        String url  = String.format("http://%s%s",clientMark,path);
        return Arrays.asList("触发降级"+url) ;
    }
}
